package com.producer.controller;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.producer.service.OutPutService;
import com.producer.util.PostMethod;
import com.producer.vo.MsgVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class OutPutController {

    @Autowired
    private OutPutService outPutService;

    @Autowired
    private PostMethod postMethod;

    /**
     * 将 数据 发送给 Kafka
     *
     * @param msgVO: 告警、性能、资源等信息
     */
    @PostMapping(value = "sendOutPut")
    public void sendOutPut(@RequestBody MsgVO msgVO) {
       /* List<MsgVO> list =new ArrayList<>();
        JSONObject object = JSON.parseObject(msgVO.getMsg());

        for (int i = 0; i < 20; i++) {
            msgVO.setUuid("uuid-"+(i+1));

            //重新造数据（适配性能采集那边）
            long currentTimeMillis=System.currentTimeMillis();
            long timestampInSeconds = currentTimeMillis / 1000;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 创建SimpleDateFormat对象，指定日期格式
            Date date = new Date(currentTimeMillis);// 将时间戳转换为Date对象
            String formattedDate = sdf.format(date);// 使用SimpleDateFormat格式化Date对象
            JSONObject cpu = new JSONObject();
            cpu.put("name", "mem");
            cpu.put("timestamp", timestampInSeconds);
            cpu.put("tags", new JSONObject()
                    .fluentPut("brandCode", object.getString("brandCode"))
                    .fluentPut("uuid", msgVO.getUuid())
                    .fluentPut("user", object.getString("user"))
                    .fluentPut("metricName", "memoryUsage")
                    .fluentPut("ipAddress", object.getString("ipAddr")));
            cpu.put("fields", new JSONObject().fluentPut("used_percent", object.getBigDecimal("mem_usage")).fluentPut("metricName","memoryUsage"));
            msgVO.setMsg(cpu.toJSONString());
            printLog(object.getString("ipAddr"),object.getString("uuid"),"mem",formattedDate);
            //outPutService.sendOutPut(msgVO);
            log.info(">>>>>>>>>>>>"+msgVO.getMsg());
                list.add(msgVO);
            postMethod.pushData(msgVO);
            //postMethod.pushData(msgVO);
        }*/

        //重新造数据（适配性能采集那边）
        JSONObject object = JSON.parseObject(msgVO.getMsg());
        long currentTimeMillis=System.currentTimeMillis();
        long timestampInSeconds = currentTimeMillis / 1000;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 创建SimpleDateFormat对象，指定日期格式
        Date date = new Date(currentTimeMillis);// 将时间戳转换为Date对象
        String formattedDate = sdf.format(date);// 使用SimpleDateFormat格式化Date对象
        log.info(">>>>>>>>>>>>"+msgVO.getMsg());
        if (StringUtils.isNotBlank(object.getString("metricName"))) {
            if (object.getString("metricName").equals("cpuUsage")) {
                JSONObject cpu = new JSONObject();
                cpu.put("name", "cpu");
                cpu.put("timestamp", timestampInSeconds);
                cpu.put("tags", new JSONObject()
                        .fluentPut("brandCode", object.getString("brandCode"))
                        .fluentPut("uuid", object.getString("uuid"))
                        .fluentPut("ipAddress", object.getString("ipAddr"))
                        .fluentPut("user", object.getString("user"))
                        .fluentPut("metricName", "cpuUsage")
                        .fluentPut("cpu", "cpu-total"));
                cpu.put("fields", new JSONObject().fluentPut("usage_idle", new BigDecimal(100).subtract(object.getBigDecimal("cpu_usage"))).fluentPut("metricName","cpuUsage"));
                msgVO.setMsg(cpu.toJSONString());
                printLog(object.getString("ipAddr"),object.getString("uuid"),"cpu",formattedDate);
                outPutService.sendOutPut(msgVO);
                postMethod.pushData(msgVO);
            }


            if (object.getString("metricName").equals("memoryUsage")) {
                JSONObject cpu = new JSONObject();
                cpu.put("name", "mem");
                cpu.put("timestamp", timestampInSeconds);
                cpu.put("tags", new JSONObject()
                        .fluentPut("brandCode", object.getString("brandCode"))
                        .fluentPut("uuid", object.getString("uuid"))
                        .fluentPut("user", object.getString("user"))
                        .fluentPut("metricName", "memoryUsage")
                        .fluentPut("ipAddress", object.getString("ipAddr")));
                cpu.put("fields", new JSONObject().fluentPut("used_percent", object.getBigDecimal("mem_usage")).fluentPut("metricName","memoryUsage"));
                msgVO.setMsg(cpu.toJSONString());
                printLog(object.getString("ipAddr"),object.getString("uuid"),"mem",formattedDate);
                outPutService.sendOutPut(msgVO);
                postMethod.pushData(msgVO);


            }


            if (object.getString("metricName").equals("diskUsage")) {
                JSONObject cpu = new JSONObject();
                cpu.put("name", "diskinfo_exec");
                cpu.put("timestamp", timestampInSeconds);
                cpu.put("tags", new JSONObject()
                        .fluentPut("brandCode", object.getString("brandCode"))
                        .fluentPut("uuid", object.getString("uuid"))
                        .fluentPut("metricName", object.getString("diskUsage"))
                        .fluentPut("ipAddress", object.getString("ipAddr")));
                cpu.put("fields", new JSONObject()
                        .fluentPut("diskTotal", object.getString("diskTotal"))
                        .fluentPut("fileTotal", StringUtils.isNotBlank(object.getString("file_total_disk"))?object.getString("file_total_disk"):object.getString("fileTotal"))
                        .fluentPut("fileUse", StringUtils.isNotBlank(object.getString("file_use_disk"))?object.getString("file_use_disk"):object.getString("fileUse"))
                        .fluentPut("ipAddr", object.getString("ipAddr"))
                        .fluentPut("user", object.getString("user"))
                        .fluentPut("metricName", object.getString("metricName"))
                        .fluentPut("diskRate", object.getString("diskRate")));
                msgVO.setMsg(cpu.toJSONString());
                printLog(object.getString("ipAddr"),object.getString("uuid"),"disk",formattedDate);
                outPutService.sendOutPut(msgVO);
                postMethod.pushData(msgVO);
            }
        }

        //Windows性能采集脚本数据处理
        if(StringUtils.isNotBlank(object.getString("os"))&&object.getString("os").contains("Windows")){
            if(StringUtils.isNotBlank(object.getString("memory_usage_percentage"))){
                JSONObject cpu = new JSONObject();
                cpu.put("name", "mem");
                cpu.put("timestamp", timestampInSeconds);
                cpu.put("tags", new JSONObject()
                        .fluentPut("brandCode", object.getString("brandCode"))
                        .fluentPut("uuid", object.getString("uuid"))
                        .fluentPut("user", object.getString("user"))
                        .fluentPut("metricName", "memoryUsage")
                        .fluentPut("ipAddress", object.getString("ipAddr")));
                cpu.put("fields", new JSONObject().fluentPut("used_percent", object.getString("memory_usage_percentage")).fluentPut("metricName","memoryUsage"));
                msgVO.setMsg(cpu.toJSONString());
                printLog(object.getString("ipAddr"),object.getString("uuid"),"mem",formattedDate);
                outPutService.sendOutPut(msgVO);
                postMethod.pushData(msgVO);
            }

            if(StringUtils.isNotBlank(object.getString("disk_usage_percentage"))){
                JSONObject cpu = new JSONObject();
                cpu.put("name", "diskinfo_exec");
                cpu.put("timestamp", timestampInSeconds);
                cpu.put("tags", new JSONObject()
                        .fluentPut("brandCode", object.getString("brandCode"))
                        .fluentPut("uuid", object.getString("uuid"))
                        .fluentPut("metricName", "diskUsage")
                        .fluentPut("ipAddress", object.getString("ipAddr")));
                cpu.put("fields", new JSONObject()
                        .fluentPut("diskTotal", object.getString("diskTotal"))
                        .fluentPut("fileTotal", object.getString("fileTotal"))
                        .fluentPut("fileUse", object.getString("fileUse"))
                        .fluentPut("ipAddr", object.getString("ipAddr"))
                        .fluentPut("user", object.getString("user"))
                        .fluentPut("metricName", "diskUsage")
                        .fluentPut("diskRate", object.getString("disk_usage_percentage")));
                msgVO.setMsg(cpu.toJSONString());
                printLog(object.getString("ipAddr"),object.getString("uuid"),"disk",formattedDate);
                outPutService.sendOutPut(msgVO);
                postMethod.pushData(msgVO);
            }


            if (StringUtils.isNotBlank(object.getString("cpu_usage_percentage"))) {
                JSONObject cpu = new JSONObject();
                cpu.put("name", "cpu");
                cpu.put("timestamp", timestampInSeconds);
                cpu.put("tags", new JSONObject()
                        .fluentPut("brandCode", object.getString("brandCode"))
                        .fluentPut("uuid", object.getString("uuid"))
                        .fluentPut("ipAddress", object.getString("ipAddr"))
                        .fluentPut("user", object.getString("user"))
                        .fluentPut("cpu", "cpu-total")
                       .fluentPut("metricName", "cpuUsage"));
                cpu.put("fields", new JSONObject().fluentPut("usage_idle", new BigDecimal(100).subtract(object.getBigDecimal("cpu_usage_percentage"))).fluentPut("metricName","cpuUsage"));
                msgVO.setMsg(cpu.toJSONString());
                printLog(object.getString("ipAddr"),object.getString("uuid"),"cpu",formattedDate);
                outPutService.sendOutPut(msgVO);
                postMethod.pushData(msgVO);
            }
        }


    }

    public void printLog(String ip,String uuid,String metricName,String formattedDate){
        if(StringUtils.isBlank(uuid)){
            log.info("UUID为空，IP地址====>"+ip);
        }
        log.info("推送时间===>"+formattedDate+"，指标===>"+metricName+"，UUID===>"+uuid);
    }
}
